package com.paic.mhis.hcpms.assessment.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Description: 日期转换工具类
 * 
 * @author: EX-ZENGYUANCHAO001
 * @version:
 */
public class AssessmentUtils {

	private static final DecimalFormat DF = new DecimalFormat("0.00");

	public AssessmentUtils() {

	}

	/**
	 * 
	 * Description: 将日期字符串转换成该日期上月字符串
	 * 
	 * @param dateStr
	 *            当前日期
	 * @param dateFromat
	 *            日期格式
	 * @return 上月日期字符串
	 * @throws ParseException
	 * 
	 *             Modification History: Date Version Author Description
	 *             --------
	 *             ----------------------------------------------------------
	 *             2015-3-11 mhis_hcpms EX-ZENGYUANCHAO001 创建
	 */
	public static String getLastMonth(String dateStr, String dateFromat) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat(dateFromat);
		String dateString = null;
		Date date = format.parse(dateStr);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, -1);
		dateString = format.format(calendar.getTime());
		return dateString;
	}

	/**
	 * 
	 * Description:bigdecimal转为字符串并保留2位小数    
	 * @param num
	 * @return
	 * 
	 * Modification History:  
	 * Date        Version       Author          Description  
	 * ------------------------------------------------------------------  
	 * 2015-3-27    mhis_hcpms     EX-YUFEI002     创建
	 */
	public static String decimalToString(BigDecimal num) {
		if (num == null) {
			return "";
		}
		String strNum = DF.format(num);
		for (int i = 0; i < 3; i++) {
			if (strNum.endsWith("0") || strNum.endsWith(".")) {
				strNum = strNum.substring(0, strNum.length() - 1);
			} else {
				break;
			}
		}
		return strNum;
	}

	public static BigDecimal decimalToDecimal(BigDecimal num) {
		if (num == null) {
			return new BigDecimal(0);
		}
		String strNum = DF.format(num);
		for (int i = 0; i < 3; i++) {
			if (strNum.endsWith("0") || strNum.endsWith(".")) {
				strNum = strNum.substring(0, strNum.length() - 1);
			} else {
				break;
			}
		}
		return new BigDecimal(strNum);
	}

	public static void main(String[] args) {
		System.out.println(decimalToString(new BigDecimal(1.00)));
		System.out.println(decimalToString(null));
		System.out.println(decimalToString(new BigDecimal(1.10)));
		System.out.println(decimalToString(new BigDecimal(1.001)));
		System.out.println(decimalToString(new BigDecimal(10.0)));
		System.out.println(decimalToString(new BigDecimal(10.0108)));
		System.out.println(decimalToString(new BigDecimal(10.0188)));
	}
}
